
//  THJFlashNewsVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2018/3/12.
//  Copyright © 2018年 河南德艺. All rights reserved.
//
#import "THJFlashNewsVC.h"
#import "JXSegment.h"
#import "JXPageView.h"
#import <MJRefresh.h>
#import "THJFlashNewsCell.h"

#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
@interface THJFlashNewsVC ()<JXSegmentDelegate,JXPageViewDataSource,JXPageViewDelegate,UITableViewDelegate,UITableViewDataSource>{
    JXPageView *pageView;
    JXSegment *segment;
    UITableView * _tableview;
    UILabel * label;
}
@property(nonatomic,strong) NSArray *channelArray;
@property(nonatomic,strong) NSMutableArray *dataArr;
@end

@implementation THJFlashNewsVC
-(NSMutableArray*)dataArr{
    if(!_dataArr){
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"快讯"];
    }else{
        [self setNavBarWithTitle:@"快讯"];
    }
//    segment = [[JXSegment alloc] initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, 40)];
//    [segment updateChannels:self.channelArray];
//
//    segment.delegate = self;
//    [self.view addSubview:segment];
    
//    pageView =[[JXPageView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT)];
//    pageView.datasource = self;
//    pageView.delegate = self;
//    [pageView reloadData];
//    [pageView changeToItemAtIndex:0];
//    [self.view addSubview:pageView];
    [self setTableview];
    [self getProductListDataWithPage:@"1"];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}
- (NSArray*)channelArray{
    if (_channelArray == nil) {
        _channelArray = @[@"硅铁",@"锰铁",@"铬铁",@"钒铁"];
    }
    return _channelArray;
}
#pragma mark - JXPageViewDataSource
-(NSInteger)numberOfItemInJXPageView:(JXPageView *)pageView{
    return self.channelArray.count;
}

-(UIView*)pageView:(JXPageView *)pageView viewAtIndex:(NSInteger)index{
    UIView *view = [[UIView alloc] init];
    [view setBackgroundColor:[self randomColor]];
    return view;
}
#pragma mark - JXSegmentDelegate
- (void)JXSegment:(JXSegment*)segment didSelectIndex:(NSInteger)index{
    [pageView changeToItemAtIndex:index];
}
#pragma mark - JXPageViewDelegate
- (void)didScrollToIndex:(NSInteger)index{
    [segment didChengeToIndex:index];
}
- (UIColor *) randomColor
{
    CGFloat hue = ( arc4random() % 256 / 256.0 );
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}
#pragma mark -getdata-
-(void)getProductListDataWithPage:(NSString*)page{
    __weak typeof(self) weakself = self;
    NSString * urlStr = [NSString stringWithFormat:@"%@%@%@&page=%@",THJUrlDomain,THJindexPHP,@"f=kuaixun&r=kuaixun",page];
    NSLog(@"快讯列表url-%@",urlStr);
    [THJNetWork GetDataWithGetRequestTypeUrl:urlStr Callback:^(NSDictionary* receiveData, NSError *error) {
        [_tableview.mj_footer endRefreshing];
        NSLog(@"receiveData-%@",receiveData);
        for (NSDictionary * dic in receiveData[@"data"]){
            [weakself.dataArr  addObject:dic];
        }
        [_tableview reloadData];
    }];
}
#pragma mark --tableview---
-(void)setTableview{
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, NavbarHeight, ScreenWidth, ScreenHeight - NavbarHeight) style:UITableViewStyleGrouped];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.backgroundColor = SingleColor(whiteColor);
    [_tableview registerNib:[UINib nibWithNibName:@"THJFlashNewsCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableview];
}
- (float) heightForString:(NSString *)value{
    CGSize strSize = [value boundingRectWithSize:CGSizeMake(ScreenWidth - 100, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size;
    return strSize.height;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *headerSectionID = @"headerSectionID";
    UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerSectionID];
    
    if (headerView == nil) {
        NSLog(@"新建的");
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:headerSectionID];
        label = [[UILabel alloc] initWithFrame:CGRectMake(2, 2, ScreenWidth, 26)];
        label.font = [UIFont systemFontOfSize:15];
        [headerView addSubview:label];
    }
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = COLOR(245, 245, 245, 1);
    NSString * text = self.dataArr[section][@"date"];
    NSString * month = [text componentsSeparatedByString:@"-"][0];
    NSString * day = [text componentsSeparatedByString:@"-"][1];
    NSLog(@"&&&KKK__LL-%@-%@",month,day);
    
    label.text = [NSString stringWithFormat:@"%@月%@日",month,day];
    NSLog(@"&&&KKK__LL-%@-%@",month,day);
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 3;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * str = self.dataArr[indexPath.section][@"msg"][indexPath.row][@"message"];
    CGFloat height = [self heightForString:str];
    return height+60;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray * arr = self.dataArr[section][@"msg"];
    return arr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    THJFlashNewsCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSString * str = self.dataArr[indexPath.section][@"msg"][indexPath.row][@"message"];
    NSString * time = self.dataArr[indexPath.section][@"msg"][indexPath.row][@"time"];
    cell.contentLab.text = str;
    cell.timeLab.text = time;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
@end
